package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.mall.config.ValidationResult;
import com.chinairi.mall.config.ValidatorImpl;
import com.chinairi.mall.controller.viewobject.TICVO;
import com.chinairi.mall.controller.viewobject.TInforCVO;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.TrackInforConfig;
import com.chinairi.mall.model.TrackInformationConfig;
import com.chinairi.mall.dao.TrackInformationConfigMapper;
import com.chinairi.mall.service.ITrackInformationConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 节点报警信息配置表 服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-08-17
 */
@Service
public class TrackInformationConfigServiceImpl extends ServiceImpl<TrackInformationConfigMapper, TrackInformationConfig> implements ITrackInformationConfigService {
    @Autowired
    private TrackInformationConfigMapper trackInformationConfigMapper;
    @Autowired
    private ValidatorImpl validator;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addPoint(TICVO ticvo) {

        ValidationResult result = validator.validate(ticvo);
        if (result.isHashErrors()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
        }

        if(checkAllExist(ticvo.getTrackNumber(), ticvo.getTicType(), ticvo.getTicName(), ticvo.getTicScope(), ticvo.getTicId())) {
            throw new BusinessException(EmBusinessError.ITEMS_Exist);
        }
        TrackInformationConfig trackInformationConfig = new TrackInformationConfig();
        BeanUtils.copyProperties(ticvo,trackInformationConfig);
        trackInformationConfig.setTicId(UUID.randomUUID().toString().replace("-",""));
        trackInformationConfigMapper.insert(trackInformationConfig);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updatePoint(TICVO ticvo) {

        ValidationResult result = validator.validate(ticvo);
        if (result.isHashErrors()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
        }

        if(checkAllExist(ticvo.getTrackNumber(), ticvo.getTicType(), ticvo.getTicName(), ticvo.getTicScope(), ticvo.getTicId())) {
            throw new BusinessException(EmBusinessError.ITEMS_Exist);
        }

        TrackInformationConfig trackInformationConfig = new TrackInformationConfig();
        BeanUtils.copyProperties(ticvo,trackInformationConfig);
        trackInformationConfigMapper.updateById(trackInformationConfig);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deletePoint(String uuid) {
        trackInformationConfigMapper.deleteById(uuid);
    }

    @Override
    @DoPage
    public List<TrackInformationConfig> getPoints(TICVO ticvo) {
        QueryWrapper<TrackInformationConfig> wrapper = new QueryWrapper<>();
        wrapper
                .eq(StringUtils.isNotEmpty(ticvo.getTrackNumber()),"TRACK_NUMBER",ticvo.getTrackNumber());
        List<TrackInformationConfig> list = trackInformationConfigMapper.selectList(wrapper);
        return list;
    }

    public boolean checkAllExist(String number, String type, String name, Integer scope, String id) {
        return trackInformationConfigMapper.countByAll(number,type,name,scope,id) > 0;
    }
}
